import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    dialogFormVisible: false,
    isLogin: false,
    userId: 0,
    userNickName: null,
    roles: null,
    token: ''
  },
  mutations: {
    //打开登录框
    showLoginBox(state) {
      state.dialogFormVisible = true
    },
    //关闭登录框
    closeLobinBox(state) {
      state.dialogFormVisible = false
    },
    //登录成功后更改用户状态
    upUserState(state, userData) {
      if(userData) {
        state.isLogin = true
        state.userId = userData.id
        state.userNickName = userData.nickName
        state.roles = userData.roles
      } else if(userData == null) {
        //登出时清空sessionStorage里的东西
        sessionStorage.removeItem('userDetail')
        state.isLogin = false
        state.userId = 0
        state.userNickName = null
        state.roles = null
      }
    }
  },
  actions: {
    setUser({commit}, user) {
      commit('upUserState', user)
    }
  },
  getters: {
    getNickName(state) {
      return state.userNickName
    },
    getIsLogin(state) {
      return state.isLogin
    }
  },
  modules: {
  }
})
